﻿using JetBrains.Annotations;
using System;
using System.Diagnostics;
using System.Reflection;

namespace Xms.Abp.Data;

public class DynamicConnStringNameAttribute : ConnStringNameAttribute
{
    [NotNull]
    public override string Name { get; }

    public DynamicConnStringNameAttribute(
            [NotNull] Type sourceType,
            [CanBeNull] string connectionStringNamePropertyName)
    {
        var connectionStringNameProperty = sourceType.GetProperty(
            connectionStringNamePropertyName,
            BindingFlags.Static | BindingFlags.Public
        );
        Debug.Assert(connectionStringNameProperty != null, nameof(connectionStringNameProperty) + " != null");
        Name = (string)connectionStringNameProperty.GetValue(null);
    }
}
